#!/usr/bin/perl

=head1 NAME

stats.pl - create a chart of email stats

=head1 DESCRIPTION

Creates a neat looking chart of email stats using log information havested
from filter.pm

=head1 DEPENDENCIES

L<MLDBM>

=cut

require configvars;                  # Found via PERL5LIB env variable
use MLDBM qw( DB_File Storable );    # use Storable for serializing
use Fcntl qw(:DEFAULT);
use strict;

=head1 SYNOPSIS

  perl stats.pl

=cut

my $tempfile = "/tmp/temphashedb";
my $epoch    = time;
my ( $perhour,   $perday,   $perweek,   $permonth,   $peryear )   = 0;
my ( $listhour,  $listday,  $listweek,  $listmonth,  $listyear )  = 0;
my ( $spamhour,  $spamday,  $spamweek,  $spammonth,  $spamyear )  = 0;
my ( $otherhour, $otherday, $otherweek, $othermonth, $otheryear ) = 0;
our ( %database, %temphash );

if ( &GetFileTime > 1800 ) {

    tie %temphash, 'MLDBM', $tempfile, O_CREAT | O_RDWR, 0640 or die "Cannot open $tempfile $!\n";
    tie %database, 'MLDBM', $MauchConfig::maildatabase, O_CREAT | O_RDWR, 0640 or die "Cannot open $MauchConfig::maildatabase $!\n";

    for my $record ( keys %database ) {
        my $ref  = $database{ $record };
        my $date = $ref->{ 'date' };
        my $type = $ref->{ 'type' };

        if ( $type eq "personal" ) {
            if ( $date > ( $epoch - 31536000 ) ) {
                $peryear = $peryear + 1;
                if ( $date > ( $epoch - 2592000 ) ) {
                    $permonth = $permonth + 1;
                    if ( $date > ( $epoch - 604800 ) ) {
                        $perweek = $perweek + 1;
                        if ( $date > ( $epoch - 86400 ) ) {
                            $perday = $perday + 1;
                            if ( $date > ( $epoch - 3600 ) ) {
                                $perhour = $perhour + 1;
                            }
                        }
                    } ## end if ( $date > ( $epoch ...
                } ## end if ( $date > ( $epoch ...
            } ## end if ( $date > ( $epoch ...
        } elsif ( $type eq "lists" ) {
            if ( $date > ( $epoch - 31536000 ) ) {
                $listyear = $listyear + 1;
                if ( $date > ( $epoch - 2592000 ) ) {
                    $listmonth = $listmonth + 1;
                    if ( $date > ( $epoch - 604800 ) ) {
                        $listweek = $listweek + 1;
                        if ( $date > ( $epoch - 86400 ) ) {
                            $listday = $listday + 1;
                            if ( $date > ( $epoch - 3600 ) ) {
                                $listhour = $listhour + 1;
                            }
                        }
                    } ## end if ( $date > ( $epoch ...
                } ## end if ( $date > ( $epoch ...
            } ## end if ( $date > ( $epoch ...
        } elsif ( $type eq "spam" ) {
            if ( $date > ( $epoch - 31536000 ) ) {
                $spamyear = $spamyear + 1;
                if ( $date > ( $epoch - 2592000 ) ) {
                    $spammonth = $spammonth + 1;
                    if ( $date > ( $epoch - 604800 ) ) {
                        $spamweek = $spamweek + 1;
                        if ( $date > ( $epoch - 86400 ) ) {
                            $spamday = $spamday + 1;
                            if ( $date > ( $epoch - 3600 ) ) {
                                $spamhour = $spamhour + 1;
                            }
                        }
                    } ## end if ( $date > ( $epoch ...
                } ## end if ( $date > ( $epoch ...
            } ## end if ( $date > ( $epoch ...
        } elsif ( $type eq "other" ) {
            if ( $date > ( $epoch - 31536000 ) ) {
                $otheryear = $otheryear + 1;
                if ( $date > ( $epoch - 2592000 ) ) {
                    $othermonth = $othermonth + 1;
                    if ( $date > ( $epoch - 604800 ) ) {
                        $otherweek = $otherweek + 1;
                        if ( $date > ( $epoch - 86400 ) ) {
                            $otherday = $otherday + 1;
                            if ( $date > ( $epoch - 3600 ) ) {
                                $otherhour = $otherhour + 1;
                            }
                        }
                    } ## end if ( $date > ( $epoch ...
                } ## end if ( $date > ( $epoch ...
            } ## end if ( $date > ( $epoch ...
        } ## end elsif ( $type eq "other" )
    } ## end for my $record ( keys %database)

    my $personal = $temphash{'personal'};
    $personal->{'hour'} = $perhour;
    $personal->{'day'}  = $perday;
    $personal->{'week'} = $perweek;
    $personal->{'month'} = $permonth;
    $personal->{'year'} = $peryear;
    $temphash{'personal'} = $personal;

    my $list = $temphash{'list'};
    $list->{'hour'} = $listhour;
    $list->{'day'}  = $listday;
    $list->{'week'} = $listweek;
    $list->{'month'} = $listmonth;
    $list->{'year'} = $listyear;
    $temphash{'list'} = $list;
    
    my $other = $temphash{'other'};
    $other->{'hour'} = $otherhour;
    $other->{'day'}  = $otherday;
    $other->{'week'} = $otherweek;
    $other->{'month'} = $othermonth;
    $other->{'year'} = $otheryear;
    $temphash{'other'} = $other;

    my $spam = $temphash{'spam'};
    $spam->{'hour'} = $spamhour;
    $spam->{'day'}  = $spamday;
    $spam->{'week'} = $spamweek;
    $spam->{'month'} = $spammonth;
    $spam->{'year'} = $spamyear;
    $temphash{'spam'} = $spam;

    untie %database;
    untie %temphash;
} else {

    tie %temphash, 'MLDBM', $tempfile, O_CREAT | O_RDWR, 0640 or die "Cannot open $tempfile $!\n";

    $perhour = $temphash{'personal'}{'hour'};
    $perday = $temphash{'personal'}{'day'};
    $perweek = $temphash{'personal'}{'week'};
    $permonth = $temphash{'personal'}{'month'};
    $peryear = $temphash{'personal'}{'year'};
    
    $listhour = $temphash{'list'}{'hour'};
    $listday = $temphash{'list'}{'day'};
    $listweek = $temphash{'list'}{'week'};
    $listmonth = $temphash{'list'}{'month'};
    $listyear = $temphash{'list'}{'year'};
    
    $otherhour = $temphash{'other'}{'hour'};
    $otherday = $temphash{'other'}{'day'};
    $otherweek = $temphash{'other'}{'week'};
    $othermonth = $temphash{'other'}{'month'};
    $otheryear = $temphash{'other'}{'year'};
    
    $spamhour = $temphash{'spam'}{'hour'};
    $spamday = $temphash{'spam'}{'day'};
    $spamweek = $temphash{'spam'}{'week'};
    $spammonth = $temphash{'spam'}{'month'};
    $spamyear = $temphash{'spam'}{'year'};

    untie %temphash;
}

print "\n";
print "Email    \| Hr  \| Day \| Week \| Month \| Year\n";
print "--------- ----- ----- ------ ------- ------\n";
printf( "Personal \| %.3d \| %.3d \| %.4d \| %.5d \| %.5d\n", $perhour,   $perday,   $perweek,   $permonth,   $peryear );
printf( "Lists    \| %.3d \| %.3d \| %.4d \| %.5d \| %.5d\n", $listhour,  $listday,  $listweek,  $listmonth,  $listyear );
printf( "Other    \| %.3d \| %.3d \| %.4d \| %.5d \| %.5d\n", $otherhour, $otherday, $otherweek, $othermonth, $otheryear );
printf( "Spam     \| %.3d \| %.3d \| %.4d \| %.5d \| %.5d\n", $spamhour,  $spamday,  $spamweek,  $spammonth,  $spamyear );

exit( 0 );

sub GetFileTime {
    my ( $dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size, $atime, $mtime, $ctime, $blksize, $blocks ) = stat( $tempfile );
    my $diff = time - $mtime;
  return $diff;
}

=head1 BUGS AND LIMITATIONS

No bugs have been reported.

Please report any bugs or feature requests to C<cmauch@gmail.com>

=head1 AUTHOR

Charles Mauch <cmauch@gmail.com>

=head1 LICENSE

Copyright (c) 2006 Charles Mauch

This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; either version 2 of the License, or (at your option) any later
version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.  See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
Street, Fifth Floor, Boston, MA  02110-1301, USA.

=head1 SEE ALSO

perl(1).

=cut

# $Id: stats.pl 87 2006-10-10 09:59:58Z cmauch $
